package cn.com.qc.leeactivity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.qc.R;
import cn.com.qc.adapter.ProAdapter;
import cn.com.qc.help.NetUrl;
import cn.com.qc.javabean.JobInfo;
import cn.com.qc.javabean.ProInfo;
import cn.com.qc.main.BaseActivity;
import cn.com.qc.utils.CustomListView;
import cn.com.qc.utils.Tools;

/**
 * 公司信息
 */

public class CharacterActivity extends BaseActivity {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.code)
    TextView code;
    @BindView(R.id.color_name)
    TextView color_name;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.listView)
    CustomListView listView;
    private String codes = "";
    private List<ProInfo> proInfoList;
    private ProAdapter proAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_character);
        ButterKnife.bind(this);
        Tools.initSystemBarTint(this);
        codes = getIntent().getStringExtra("code");
        code.setText(codes);
        proInfoList = new ArrayList<>();
        initData();
        proAdapter = new ProAdapter(proInfoList, this);
        listView.setAdapter(proAdapter);
    }

    private void initData() {
        OkGo.<String>post(NetUrl.DNS + NetUrl.Character)
                .tag(this)
                .params("code", codes)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            System.out.println(response.body());
                            JSONObject jsonObject = new JSONObject(response.body());
                            int status = jsonObject.getInt("status");
                            if (status == 0) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                JSONObject character = data.optJSONObject("character");
                                if (character!=null){
                                    color_name.setText(character.optString("name"));
                                    content.setText(character.optString("content"));
                                }
                                JSONArray proList = data.optJSONArray("proList");
                                proInfoList = new ArrayList<>();
                                for (int i = 0; i < proList.length(); i++) {
                                    JSONObject jsonObject2 = proList.getJSONObject(i);
                                    ProInfo proInfo = new ProInfo();
                                    proInfo.setCharacterCode(jsonObject2.optString("characterCode"));
                                    proInfo.setName(jsonObject2.optString("name"));
                                    proInfo.setDetails(jsonObject2.optString("details"));
                                    proInfo.setId(jsonObject2.optInt("id"));
                                    proInfoList.add(proInfo);
                                }
                                proAdapter.addData(proInfoList);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
